package ws.very.util.dingtalk

import java.io.{File, FileWriter}
import java.nio.file.{FileStore, Files}

import scala.io.{Source, StdIn}

trait DingTalkEnv {
  def OAPI_HOST = "https://oapi.dingtalk.com"
  def CORP_ID:S
  def SSO_Secret:S

  val CREATE_SUITE_KEY = "suite4xxxxxxxxxxxxxxx"
  def SUITE_KEY:S
  def SUITE_SECRETE:S
  def TOKEN:S
  def ENCODING_AES_KEY:S

  var suiteTicket = ""
  var suiteToken = ""

  protected def initSuiteTicketAndToken = {
    val file = new File("persis_suite")
    println(s"suite id file is ${file.getAbsolutePath}")
    if(file.exists()){
        val source = Source.fromFile(file)
        val data = source.mkString
      if(data){
        val Array(suiteTicket,suiteToken) = data.split(" ")
        this.suiteTicket = suiteTicket
        this.suiteToken = suiteToken
      }
    }

  }
  def changeSuite(suiteTicket:S,suiteToken:S) {
    val file = new File("persis_suite")
    this.suiteTicket = suiteTicket
    this.suiteToken = suiteToken
    if (!file.exists()) {
      file.createNewFile()
    }
    val fileWriter = (new FileWriter(file))
    fileWriter.write(s"$suiteTicket $suiteToken")
    fileWriter.flush()
    fileWriter.close()
  }
  initSuiteTicketAndToken

}
